<?php
/**
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */
class control
{
    // 模板变量
    var $t_var = array();
    
    // 控制器名
	protected $title;
    
    /**
     * 操纵数据的表名
     * @var type 
     */
    protected $table;
    
    /**
     *
     * @var array 自动加载类
     */
    protected $class = array();
    
    function __construct()
	{
        $this->db = YF_f::get_db_link();
        
		foreach ($this->class as $k=>$v)
			$this->$k = YF_f::initC($v);
	}

    /**
     *
     * @var string 布局
     */
    protected $dispLayout = 'main';
	
	function display($f=NULL)
	{
        e_debug();
        if ($f===NULL){
            $this->t_var['body'] = ob_get_clean();
            return $this->loadTemplate( 'dispLayout'. DIRECTORY_SEPARATOR . $this->dispLayout);
         }
        if ($f){
            $this->t_var['body'] = ob_get_clean();
            $this->loadTemplate( 'dispLayout'. DIRECTORY_SEPARATOR . $f);
            die;
         }
        html::showHead();
        die;
	}
    
    /**
     * 前台展示布局
     * @param type $id
     * @return type
     */
    function loadLayout($id)
    {
        $data = \YF_f::load_cache('layout\v', 'layout/'. $id .'.php', $id);
        if (!$data)
            $this->clue(lang::g('layout non-existent'), '#');

        if ($data['config']['css'])
            \html::pushHead($data['config']['css'], TRUE);

        $this->t_var['g_plate_idA'] = $data['plate_ids'];
        $this->t_var['g_layout'] = $data;
		
        if ($data['tpl'])
            $this->dispLayout = $data['tpl'];
    }
    
    /**
     * 加载模板文件
     * @param string $f  加载模板文件
     * @param array $p  模板用的变量
     * @return $this
     */
    function loadTemplate($f, $p=array())
	{
        $f = self::$template_dir . $f . '.php';
		if(!is_file($f))
		 {
			YF_f::error( lang::g('template not exist:'). $f );
			return $this;
		 }
		$this->t_var && extract($this->t_var);
		$p && extract($p, EXTR_PREFIX_INVALID, 'tv');
		include $f;
        return $this;
	}
    static $template_dir = TEMPLATE_DIR_F;  // 模板目录，分支类继承
    
    
    /**
     * 从 $_POST, $_GET 取值
     * 
     * @param type $key
     * @return type
     */
    function P_G($key)
    {
        if (isset($_POST[$key]))
            return $_POST[$key];
        return $_GET[$key]; 
    }
    

    /**
     * 提示信息, 并中断
     * 
     * @param string $msg
     * @param array $jumpA
     * @param int $seconds
     */
	function clue($msg, $jumpA=null, $seconds=2)
	{
		if (is_array($jumpA))
		 {
			$jump_url = key($jumpA);
		 }
		else{
			if ($jumpA=='#')
				$jump_url='';
			elseif ($jumpA)
				$jump_url = $jumpA;
			else
				$jump_url = 'javascript:history.go(-1)';
			$jumpA = array();
		 }
         
        if ($_SERVER["HTTP_X_REQUESTED_WITH"]=="XMLHttpRequest")
            ajax_json (array('status' =>$msg, 'jumpA'=>$jumpA));
        
        $this->t_var['msg'] = $msg;
        $this->t_var['jump_url'] = $jump_url;
        $this->t_var['seconds'] = $seconds;
        $this->t_var['jumpA'] = $jumpA;
        $this->display('clue');
	}
    
	
	function isExist($d)
	{
		if ($d)return;      
        $this->clue(lang::g('page lost'), array(''=>'',ROOT_URL=>  \YF_f::$config['companyName']));
	}
}
?>
